package com.aster.common.api.controller;

import com.aster.common.api.domain.TbEscortUserInfo;
import com.aster.common.api.service.EscortUserInfoService;
import com.aster.common.api.wechat.domain.WeChatOpenId;
import com.aster.common.api.wechat.util.WeChatUtil;
import com.aster.common.base.BaseController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;


/**
 * 
 * <br>
 * <b>功能：</b>微信陪护员端首页<br>
 *   <br>
 */ 
@RestController @RequestMapping("/escortIndex")
public class WechatEscortIndexController extends BaseController {
	
	private final static Logger log = Logger.getLogger(WechatEscortIndexController.class);

	@Autowired(required=false)
	private EscortUserInfoService escortUserInfoService;

	@RequestMapping("/getCode")
	public void getCode(HttpSession session, String code) {
		if (code != null) {
			log.info("获得微信code："+code);
			WeChatOpenId content = WeChatUtil.getWeChatOpenid(code);
			String openId = content.getOpenid();
			String access_token = content.getAccess_token();
			log.info("获得Openid：" + openId + "||" + new Date());
			TbEscortUserInfo tbEscortUserInfo;
			if (openId != null) {
				tbEscortUserInfo = escortUserInfoService.getEscortByOpenId(openId);
				if (tbEscortUserInfo!=null){
					//该陪护员已绑定微信，允许进入主页，并缓存该用户信息
					log.info("该陪护员已绑定微信");
				}else {
					//该陪护员未绑定微信，跳转至登录页面
					log.info("该陪护员未绑定微信，跳转至登录页面");
				}

			}
		} else {

		}

	}


}
